#include "2440addr.h"
#define LOOP 10000
#define ADC_FREQ 2500000
volatile U32 preScaler= 50000000/ADC_FREQ -1;

//==================================================================================		
int ReadAdc(int ch)
{
    int i;
    static int prevCh=-1;

    rADCCON = (1<<14)|(preScaler<<6)|(ch<<3);	//setup channel

    if(prevCh!=ch)
    {
	rADCCON = (1<<14)|(preScaler<<6)|(ch<<3);   //setup channel
	for(i=0;i<LOOP;i++);	//delay to set up the next channel
	prevCh=ch;
    }
    rADCCON|=0x1;   //start ADC

    while(rADCCON & 0x1); 	//check if Enable_start is low
    while(!(rADCCON & 0x8000));	//check if EC(End of Conversion) flag is high

    return ( (int)rADCDAT0 & 0x3ff );
}